package com.best.member.controller;

import javax.servlet.http.HttpServletRequest;

import com.best.core.constants.CommonConstants;
import com.best.core.utils.JsonUtils;
import com.best.core.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.best.core.entity.CommonResult;
import com.best.core.utils.IpUtils;
import com.best.member.client.WeiXinClient;
import com.best.member.input.dto.UserLoginInpDTO;
import com.best.member.input.dto.UserRegisterDTO;
import com.best.member.output.dto.UserOutputDTO;
import com.best.member.serice.MemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @ClassName: MemberController
 * @Package: com.best.member.controller
 * @Description: 会员服务接口
 * @Datetime: 2020/6/5 16:30
 * @Author: nike
 */
@Slf4j
@CrossOrigin
@Api(tags = "会员服务接口")
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private WeiXinClient weiXinClient;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "会员服务测试方法")
    @GetMapping("/test")
    public String printTest() {
        return "member test";
    }

    @ApiOperation(value = "会员服务调用微信服务测试方法")
    @GetMapping("/weixin")
    public String testWeiXinClient() {
        return weiXinClient.testFeign();
    }

    /**
     * 用户注册接口
     * 
     * @param userRegisterDTO 注册输入参数
     * @param bindingResult 绑定参数检验
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "会员用户注册信息接口")
    public CommonResult register(@RequestBody @Validated UserRegisterDTO userRegisterDTO,
            BindingResult bindingResult) {
        log.info("register params is : {}", userRegisterDTO);
        // 校验
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                return CommonResult.buildFail(error.getDefaultMessage());
            }
        }
        return memberService.register(userRegisterDTO);
    }

    /**
     * 根据手机号码查询是否已经存在,如果存在返回当前用户信息
     * 
     * @param mobile
     * @return
     */
    @ApiOperation(value = "根据手机号码查询是否已经存在")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "mobile", dataType = "String",
            required = true, value = "用户手机号码"),})
    @PostMapping("/verifyMobile")
    public CommonResult<UserOutputDTO> verifyMobile(@RequestParam("mobile") String mobile) {
        return memberService.verifyMobile(mobile);
    }

    /**
     * 用户登录
     * 
     * @param userLoginInpDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "会员用户登录接口")
    public CommonResult login(@RequestBody @Validated UserLoginInpDTO userLoginInpDTO,
            BindingResult bindingResult, HttpServletRequest request) {
        log.info("login params is : {}", userLoginInpDTO);
        // 校验
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                return CommonResult.buildFail(error.getDefaultMessage());
            }
        }
        //校验验证码
        String redisKey = userLoginInpDTO.getRedisKey();
        if(StringUtils.isEmpty(redisKey)){
            return CommonResult.buildFail("验证码已失效，请重新获取！");
        }
        String redisCode = redisUtil.getString(redisKey);
        if(!redisCode.equals(userLoginInpDTO.getImgCode())){
            return CommonResult.buildFail("验证码输入有误，请重新输入！");
        }
        //校验登录类型
        String loginType = userLoginInpDTO.getLoginType();
        if (!(loginType.equals(CommonConstants.MEMBER_LOGIN_TYPE_ANDROID)
                || loginType.equals(CommonConstants.MEMBER_LOGIN_TYPE_IOS)
                || loginType.equals(CommonConstants.MEMBER_LOGIN_TYPE_PC))) {
            return CommonResult.buildFail("登录类型出现错误!");
        }

        // 获取请求的IP地址
        String ipAddr = IpUtils.getIpAddr(request);
        return memberService.login(userLoginInpDTO, ipAddr);
    }

    /**
     * 根据token查询用户信息
     * 
     * @param token
     * @return
     */
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "根据token查询用户信息")
    public CommonResult<UserOutputDTO> getUserInfo(@RequestParam("token") String token) {
        return memberService.getUserInfo(token);
    }

}
